from bs4 import BeautifulSoup
from .base import Handler
from event_engine.event_type import *
from event_engine.event import Event
from event_engine.engine import EventEngine
from models.task import *


class SecondaryCircleHandler(Handler):
    """
    获取二手房商圈
    """

    def process(self, event: Event, engine: EventEngine) -> None:
        html: str = event.data["html"]
        task: Task = event.data["task"]

        soup = BeautifulSoup(html, 'html.parser')
        div = soup.find("div", attrs={"class": "position"})
        div = div.find("div", attrs={"data-role": "ershoufang"})
        div = div.findChild("div")
        div = div.find_next_sibling("div")
        if div:
            a_list = div.find_all("a")
            for a in a_list:
                url: str = task.get_host() + a.attrs["href"]
                engine.put(Event(EVENT_SAVE_TASK, {
                    "type": TYPE_SECONDARY_LIST,
                    "url": url,
                    "referer": task.url,
                }))

        task.complete()
